package com.example.service.impl;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONUtil;
import com.example.dto.XmlDTO;
import com.example.service.ItrafficFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Map;

/**
 * @ClassName: ItrafficFlowServiceImpl
 * @Author : 13736
 * @Date :2023/7/8  11:51
 * @Description: TODO
 * @Version :1.0
 */
@Slf4j
@Service
public class ItrafficFlowServiceImpl implements ItrafficFlowService {
    @Override
    public Document getXml() {
        XmlDTO xmlDTO = getXmlBean();
        log.info(JSONUtil.toJsonStr(xmlDTO.getRoot()));
        Map<String,Object> map = JSONUtil.toBean(JSONUtil.toJsonStr(xmlDTO.getRoot()),Map.class);
        return XmlUtil.mapToXml(map,"Root");
    }

    @Override
    public String getXmlStr() {
        Document doc = getXml();
        Element rootEle = XmlUtil.getRootElement(doc);
        rootEle.setAttribute("Type","Test");
        Element person = XmlUtil.getElement(rootEle,"Person");
        person.setAttribute("Name","张三");
        person.setAttribute("Age","56");
        return XmlUtil.toStr(doc);
    }

    private XmlDTO getXmlBean(){

        XmlDTO.RootBean.PersonBean.AddressBean addressBean = XmlDTO.RootBean.PersonBean.AddressBean.builder()
                .City("杭州")
                .State("萧山区")
                .Street("建设四路")
                .Zip(123456)
                .build();

        XmlDTO.RootBean.PersonBean personBean = XmlDTO.RootBean.PersonBean.builder()
                .Name("刘强东")
                .DOB("2023-07-07")
                .Address(addressBean)
                .build();

        XmlDTO.RootBean rootBean= XmlDTO.RootBean.builder()
                .Person(personBean)
                .build();

        XmlDTO xmlDTO = XmlDTO.builder()
                .Root(rootBean)
                .build();
        return xmlDTO;
    }


}
